//
//  GGHomeVC.swift
//  DongBaoDemo
//
//  Created by 陈志勇 on 2018/8/4.
//  Copyright © 2018年 陈志勇. All rights reserved.
//

import UIKit

let GGScreenW = UIScreen.main.bounds.width
let GGScreenH = UIScreen.main.bounds.height

let GGToolBarHeight = CGFloat(44.0)
let GGPlaceHolderHeight = CGFloat(144.0)

class GGHomeVC: UIViewController {

    var canScroll = true
    var collectionView: UICollectionView?
    var point = CGPoint.zero
    
    @IBOutlet weak var tableView: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        navigationController?.navigationBar.lt_setBackgroundColor(backgroundColor: .clear)
        
        setupTableView()
        setupCollectionView()
        addChildVc()
        
        NotificationCenter.default.addObserver(self, selector: #selector(childTableViewLeaveTop), name: NSNotification.Name(rawValue: "leaveTop"), object: nil)
        
        title = "DongBao"

    }

    
}

// MARK: - UI设置
extension GGHomeVC {
    
    func setupTableView() {
        
        tableView.delegate = self
        tableView.dataSource = self
        tableView.showsVerticalScrollIndicator = false
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cellID")
    }
    
    
    func setupCollectionView() {
        let layout = UICollectionViewFlowLayout()
        let height = GGScreenH - 64 - GGToolBarHeight - (self.tabBarController?.tabBar.frame.size.height)!
        layout.itemSize = CGSize(width: GGScreenW, height: height)
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.sectionInset = UIEdgeInsets.zero
        layout.scrollDirection = .horizontal
        let collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: GGScreenW, height: height), collectionViewLayout: layout)
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.isPagingEnabled = true
        collectionView.bounces = false
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "collectionCell")
        self.collectionView = collectionView
    }
    
    func addChildVc() {
        let frame = CGRect(x: 0, y: 0, width: GGScreenW, height: GGScreenH - 64 - GGToolBarHeight - (self.tabBarController?.tabBar.frame.size.height)!)
        let vc1 = GGMatchesVC()
        vc1.view.frame = frame
        
        let vc2 = GGScheduleVC()
        vc2.view.frame = frame
        
        let vc3 = GGBaseTableVC()
        vc3.view.frame = frame
        
        let vc4 = GGBaseTableVC()
        vc4.view.frame = frame
        
        addChildViewController(vc1)
        addChildViewController(vc2)
        addChildViewController(vc3)
        addChildViewController(vc4)
    }
}

// MARK: - 通知处理
extension GGHomeVC {
    @objc func childTableViewLeaveTop() {
        canScroll = true
        guard let collectOffsetX = collectionView?.contentOffset.x else {
            fatalError()
        }
        let index = Int(collectOffsetX / GGScreenW)
        let vc = childViewControllers[index] as! GGBaseTableVC
        vc.canScroll = false
        
    }
}

// MARK: - tableview数据源及代理
extension GGHomeVC: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let titleView = GGTitleView(frame: CGRect(x: 0, y: 0, width: GGScreenW, height: GGToolBarHeight), titles: ["赛事活动", "直播互动", "运动球队", "协会机构"])
        titleView.backgroundColor = .black
        titleView.didSelectTitle {[weak self] (index) in
            let indexpath = IndexPath(row: index, section: 0)
            self?.collectionView?.scrollToItem(at: indexpath, at: .left, animated: true)
        }
        return titleView
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return GGToolBarHeight
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return GGScreenH - 64 - GGToolBarHeight - (self.tabBarController?.tabBar.frame.size.height)!
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cellID", for: indexPath)
        guard let view = collectionView else {
            fatalError()
        }
        cell.contentView.addSubview(view)
        return cell
    }

}

extension GGHomeVC: UICollectionViewDataSource, UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        print("childViewControllers.count \(childViewControllers.count)")
        return childViewControllers.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let vc = childViewControllers[indexPath.row]
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath)
        cell.contentView.addSubview(vc.view)
        return cell
    }
    
}

// MARK: - scrollview
extension GGHomeVC {
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let maxTopY = tableView.rect(forSection: 0).origin.y - 64
        if scrollView.contentOffset.y >= maxTopY {
            scrollView.contentOffset = CGPoint(x: 0, y: maxTopY)
            if canScroll {
                canScroll = false
                guard let collectOffsetX = collectionView?.contentOffset.x else {
                    fatalError()
                }
                let index = Int(collectOffsetX / GGScreenW)
                let vc = childViewControllers[index] as! GGBaseTableVC
                vc.canScroll = true
            }
        }else {
            if canScroll == false {
                scrollView.contentOffset = CGPoint(x: 0, y: maxTopY)
            }
        }
        
        
        let color = UIColor(red: 0.0 / 255.0, green: 0 / 255.0, blue: 0 / 255.0, alpha: 1)
        let offsetY = scrollView.contentOffset.y
        if offsetY > 10 {
            let alpha = min(1.0, 1.0 - ((10.0 + 20.0 - Double(offsetY))  / 20.0))
            navigationController?.navigationBar.lt_setBackgroundColor(backgroundColor: color.withAlphaComponent(CGFloat(alpha)))
        }else {
            navigationController?.navigationBar.lt_setBackgroundColor(backgroundColor: color.withAlphaComponent(0))
        }
        
    }
    
}


